package com.nue.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nue.pojo.UmsRole;
import com.nue.mapper.UmsRoleMapper;
import com.nue.pojo.UmsUser;
import com.nue.service.IUmsRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nue.util.CommonResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author wk
 * @since 2020-08-19
 */
@Service
public class UmsRoleServiceImpl extends ServiceImpl<UmsRoleMapper, UmsRole> implements IUmsRoleService {


    public CommonResult list(UmsRole umsRole) {
        System.out.println(umsRole);
        // 如果 有name传过来 就按照name 模糊查询
        QueryWrapper<UmsRole> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(umsRole.getName())) {
            wrapper.like("name",umsRole.getName());
        }
        if(umsRole.getActive() != null) {
            wrapper.eq("active",umsRole.getActive());
        }
        // 如果分页返回 IPage 如果不分页 返回 List
        if(umsRole.getIfpage() == 1) {
            return CommonResult.success(this.page(new Page<>(umsRole.getPageNo(),umsRole.getPageSize()),wrapper));
        } else {
            return CommonResult.success(this.list(wrapper));
        }

    }

    @Override
    public CommonResult saveOne(UmsRole user) {
        QueryWrapper<UmsRole> qu=new QueryWrapper<>();
        qu.eq("name",user.getName());
        //System.out.println(p);
        UmsRole u=getOne(qu);
        if(u!=null){
            return CommonResult.failed("角色名已存在");
        }
        user.setActive(1);
        boolean b=save(user);
        System.out.println("此时的b"+b);
        if(b)
            return CommonResult.success("创建成功");
        else
            return CommonResult.failed();
    }

    @Override
    public CommonResult update(UmsRole user) {
        UmsRole u=new UmsRole();
        u.setId(user.getId());
        u.setName(user.getName());
        if(updateById(u)){
            return CommonResult.success("ok");
        }
        return CommonResult.failed();
    }

    @Override
    public CommonResult back(Integer id) {
        UmsRole user=new UmsRole();
        user.setId(id);
        user.setActive(1);
        boolean i=this.updateById(user);
        if(i){
            return CommonResult.success("恢复成功");
        }
        return CommonResult.failed();
    }

    @Override
    public CommonResult del(Integer id) {
        UmsRole user =new UmsRole();
        user.setId(id);
        user.setActive(0);
        boolean i=this.updateById(user);
        if(i)return CommonResult.success("删除成功");
        return CommonResult.failed();
    }
    @Override
    public boolean batchdel(Integer[] ids) {
        List<Integer> list = new ArrayList<>(ids.length);
        Collections.addAll(list,ids);
        UmsRole role = new UmsRole();
        role.setActive(0);
        UpdateWrapper<UmsRole> wrapper = new UpdateWrapper<>();
        wrapper.in("id",list);
        return this.update(role,wrapper);
    }

}
